package slb

import (
	"duanju-mq/pkg/aliyun"
	"duanju-mq/pkg/config"
	"encoding/json"
)

var (
	fEndpoint = config.String("aliyun.slb.endpoint", "slb.aliyuncs.com", "阿里云 SLB API域名")
)

//

type getAllResp struct {
	PageNumber    int
	TotalCount    int
	PageSize      int
	LoadBalancers struct {
		LoadBalancer []*LoadBalancerDetail
	}
}

type LoadBalancerDetail struct {
	LoadBalancerId     string
	LoadBalancerName   string
	LoadBalancerStatus string
	Address            string
	AddressType        string
	RegionId           string
	RegionIdAlias      string
	VSwitchId          string
	VpcId              string
	NetworkType        string
	MasterZoneId       string
	SlaveZoneId        string
	InternetChargeType string
	CreateTime         string
	CreateTimeStamp    int64
	PayType            string
	ResourceGroupId    string
	AddressIPVersion   string
}

func GetAllBalancers() ([]*LoadBalancerDetail, error) {
	list := []*LoadBalancerDetail{}
	page := 1
	for {
		resp := &getAllResp{}
		if err := aliyun.ReqApi(*fEndpoint, "DescribeLoadBalancers", map[string]any{
			"Version":    "2014-05-15",
			"RegionId":   aliyun.Region(),
			"PageNumber": page,
			"PageSize":   100,
		}, resp); err != nil {
			return nil, err
		}
		if resp.LoadBalancers.LoadBalancer == nil {
			break
		}

		for _, r := range resp.LoadBalancers.LoadBalancer {
			list = append(list, r)
		}

		if resp.PageNumber*resp.PageSize >= resp.TotalCount {
			break
		}
		page += 1
	}

	return list, nil
}

//

type BackendServerDetail struct {
	ServerId    string
	Port        int
	Weight      int
	Type        string
	Description string
}

type VServerGroupAttribute struct {
	VServerGroupId   string
	VServerGroupName string
	LoadBalancerId   string
	BackendServers   struct {
		BackendServer []*BackendServerDetail
	}
}

func GetVServerGroupBackendServers(vsgid string) ([]*BackendServerDetail, error) {
	list := []*BackendServerDetail{}
	resp := &VServerGroupAttribute{}
	if err := aliyun.ReqApi(*fEndpoint, "DescribeVServerGroupAttribute", map[string]any{
		"Version":        "2014-05-15",
		"RegionId":       aliyun.Region(),
		"VServerGroupId": vsgid,
	}, resp); err != nil {
		return nil, err
	}
	if resp.BackendServers.BackendServer == nil {
		return list, nil
	}

	for _, r := range resp.BackendServers.BackendServer {
		list = append(list, r)
	}
	return list, nil
}

//

type BackendServer struct {
	ServerId string
	Port     int
}

func AddVServerGroupBackendServers(vsgid string, s ...*BackendServer) error {
	j, _ := json.Marshal(s)
	return aliyun.ReqApi(*fEndpoint, "AddVServerGroupBackendServers", map[string]any{
		"RegionId":       aliyun.Region(),
		"VServerGroupId": vsgid,
		"BackendServers": string(j),
	}, nil)
}

func RemoveVServerGroupBackendServers(vsgid string, s ...*BackendServer) error {
	j, _ := json.Marshal(s)
	return aliyun.ReqApi(*fEndpoint, "RemoveVServerGroupBackendServers", map[string]any{
		"RegionId":       aliyun.Region(),
		"VServerGroupId": vsgid,
		"BackendServers": string(j),
	}, nil)
}
